package itsm.isperp.module.controller.serviceCatalog;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilter;
import itsm.isperp.framework.web.request.JqGridFilterRule;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.JqGridResponse;
import itsm.isperp.module.dto.solr.SolrSrcServiceDTO;
import itsm.isperp.module.entity.incident.IcdCategory;
import itsm.isperp.module.entity.serviceCatalog.SrcCategory;
import itsm.isperp.module.entity.serviceCatalog.SrcService;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.serviceCatalog.SrcCategoryService;
import itsm.isperp.module.service.serviceCatalog.SrcServiceService;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 服务控制器
 * 
 * @author zhg
 * 
 */
@Controller
@RequestMapping("serviceCatalog/service")
public class SrcServiceController extends BaseController<SrcService> {

	/**
	 * 
	 */
	@Autowired
	protected SrcServiceService srcServiceService;

	@Autowired
	protected AppDictService appDictService;

	@Override
	public SrcServiceService getService() {
		return srcServiceService;
	}

	@Override
	public String getPageTitle() {
		return "服务目录";
	}

	/**
	 * 用户编辑界面
	 * 
	 * @return 用户编辑界面视图
	 */
	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req) {
		ModelAndView mv = this.getModelAndView();

		SrcCategoryService srcCategoryService = ContextHolder
				.getSpringBean("srcCategoryService");

		SrcService model = this.getService().findOne(id);
		if (model == null) {
			model = new SrcService();
			model.setId(HibernateUUIDGenerator.generate());
			model.setReleaseDate(new Date());

		} else {

			SrcCategory category = srcCategoryService.findOne(model
					.getCategoryId());
			if (category != null) {
				mv.addObject("categoryName", category.getName());
			}

		}

		mv.addObject("model", model);

		mv.addObject("serviceWay", appDictService.findByType("SERVICE_WAY"));
		mv.addObject("serviceResponse",
				appDictService.findByType("SERVICE_RESPONSE"));
		mv.addObject("pricingModel", appDictService.findByType("PRICING_MODEL"));
		mv.addObject("serviceStatus",
				appDictService.findByType("SERVICE_STATUS"));

		return mv;
	}

	/**
	 * 根据关键字获取关联的服务
	 * 
	 * @param keyword
	 *            关键字
	 * @param request
	 *            数据请求
	 * @return json数据结果
	 */
	@RequestMapping("searchByKeyword")
	@ResponseBody
	public String searchByKeyword(String keyword, String projectId,
			JqGridRequest request) {

		JqGridFilter filter = request.getFilter();
		List<JqGridFilterRule> rules = filter.getRules();

		for (JqGridFilterRule r : rules) {
			if ("keyword".equals(r.getField())) {
				keyword = r.getData();

			}
			if ("projectId".equals(r.getField())) {
				projectId = r.getData();
			}
		}

		JqGridResponse<SolrSrcServiceDTO> resp = new JqGridResponse<SolrSrcServiceDTO>(
				this.getService().searchServiceByKeyword(keyword, projectId,
						request));

		return JsonBuilder.buildNormalBinder().toJson(resp);

	}

	@RequestMapping("selectEdit")
	public ModelAndView selectEdit(String id, HttpServletRequest req) {

		String projectId = req.getParameter("projectId");

		if (StringUtils.isEmpty(projectId)) {
			return null;
		}

		ModelAndView mv = this.getModelAndView();
		mv.addObject("projectId", projectId);
		return mv;
	}

	@Override
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = super.manage(req);
		mv.addObject("serviceStatus",
				appDictService.findByType("SERVICE_STATUS"));

		return mv;
	}
	
	@RequestMapping("listDetailPub")
	@ResponseBody
	public PageResponse<Map<String, Object>> listDetailPub(
			JqGridRequest request, HttpServletRequest req) {
		// TODO Auto-generated method stub

		request.addFilterRule("status", JqGridFilterRuleOp.eq, "发布");
		PageResponse<Map<String, Object>> listDetailPub = super.listDetail(
				request, req);
		return listDetailPub;
	}
	
	@RequestMapping("listAutocomplete")
	@ResponseBody
	public PageResponse<SrcService> listAutocomplete(JqGridRequest request,
			HttpServletRequest req) {
		String term = req.getParameter("term");
		if (StringUtils.isNotEmpty(term)) {
			request.isPageable(false);
			request.setRows(15);
			request.setSearchString(" name LIKE '%" + term
					+ "%'");
			return this.getService().findAll(request).toPage();

		} else {
			return this.getService().findAll(request).toPage();
		}

	}
}
